package com.situation.controller;

import com.github.pagehelper.PageInfo;
import com.situation.annotation.ControllerEndpoint;
import com.situation.pojo.DO.BizConsumer;
import com.situation.pojo.DO.BizOutStock;
import com.situation.pojo.VO.OutStockVO;
import com.situation.service.BizConsumerService;
import com.situation.service.OutStockService;
import com.situation.util.RespUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;

@RestController
@RequestMapping("/api/outStock")
public class OutStockController {

    @Autowired
    private OutStockService outStockService;

    @Autowired
    private BizConsumerService consumerService;

    @ControllerEndpoint(exceptionMessage = "添加出库单失败", operation = "添加出库单")
    @PostMapping("/addOutStock")
    @RequiresPermissions({"outStock:out"})
    public RespUtil addOutStock(OutStockVO outStockVO) throws IllegalAccessException {
        if (outStockVO.getConsumerId() == null) {
            //说明现在添加物资来源
            if ("".equals(outStockVO.getName()) || outStockVO.getName() == null) {
                throw new IllegalAccessException("物资去向名不能为空");
            }
            if ("".equals(outStockVO.getContact()) || outStockVO.getContact() == null) {
                throw new IllegalAccessException("联系人不能为空");
            }
            if ("".equals(outStockVO.getAddress()) || outStockVO.getAddress() == null) {
                throw new IllegalAccessException("地址不能为空");
            }
            if ("".equals(outStockVO.getPhone()) || outStockVO.getPhone() == null) {
                throw new IllegalAccessException("联系方式不能为空");
            }
            if (outStockVO.getSort() == null) {
                throw new IllegalAccessException("排序不能为空");
            }
            BizConsumer bizConsumer = new BizConsumer();
            bizConsumer.setAddress(outStockVO.getAddress());
            bizConsumer.setContact(outStockVO.getContact());
            bizConsumer.setCreateTime(LocalDateTime.now());
            bizConsumer.setModifiedTime(LocalDateTime.now());
            bizConsumer.setName(outStockVO.getName());
            bizConsumer.setPhone(outStockVO.getPhone());
            bizConsumer.setSort(outStockVO.getSort());
            BizConsumer consumer = consumerService.insert(bizConsumer);
            outStockVO.setConsumerId(consumer.getId());
        }
        //提交发放单
        outStockService.insert(outStockVO);

        return RespUtil.ok();
    }

    /**
     * 状态改为  回收站 -> 审核
     *
     * @param outNum
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "出库单恢复失败", operation = "出库单恢复")
    @PutMapping("/back/{outNum}")
    @RequiresPermissions({"outStock:back"})
    public RespUtil back(@PathVariable("outNum") String outNum) throws IllegalAccessException {
        // 审核
        int status = 2;
        Boolean aBoolean = outStockService.updateStatus(outNum, status);
        if (aBoolean)
            return RespUtil.ok().setData(aBoolean);
        return RespUtil.fail().setData(aBoolean);
    }

    /**
     * 删除   真删   =================已测==========
     *
     * @param outNum
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "删除出库单失败", operation = "删除出库单")
    @GetMapping("/delete/{outNum}")
    @RequiresPermissions({"outStock:delete"})
    public RespUtil delete(@PathVariable("outNum") String outNum) {
        Boolean aBoolean = outStockService.deleteById(outNum);
        if (aBoolean)
            return RespUtil.ok().setData(aBoolean);
        return RespUtil.fail().setData(aBoolean);
    }

    /**
     * 查询出库明细       =======已测
     *
     * @param pageNum
     * @param pageSize
     * @param outNum
     * @return map
     * con：收货人信息
     * info：物资明细信息
     */
    @GetMapping("/detail/{outNum}")
    @RequiresPermissions({"outStock:detail"})
    public RespUtil detail(int pageNum, int pageSize, @PathVariable("outNum") String outNum) {
        PageInfo<OutStockVO> outStockVOPageInfo = outStockService.queryInfo(pageNum, pageSize, outNum);
        BizConsumer bizConsumer = outStockService.queryCon(outNum);
        HashMap<String, Object> map = new HashMap<>();
        map.put("con", bizConsumer);
        map.put("info", outStockVOPageInfo);
        return RespUtil.ok().setData(map);
    }


    /**
     * 查询所有     ==================已测==================
     *
     * @param pageNum
     * @param pageSize
     * @return pageinfo
     */
    @GetMapping("/findOutStockList")
    public RespUtil findOutStockList(int pageNum, int pageSize, BizOutStock outStock) {
        return RespUtil.ok().setData(outStockService.queryAll(pageNum, pageSize, outStock));
    }

    /**
     * 修改状态       审核变已通过     ===============已测=============
     *
     * @param outNum
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "跟新出库单失败", operation = "更新出库单")
    @PutMapping("/publish/{outNum}")
    @RequiresPermissions({"outStock:publish"})
    public RespUtil publish(@PathVariable("outNum") String outNum) throws IllegalAccessException {
        // 已通过
        int status = 0;
        Boolean aBoolean = outStockService.updateStatus(outNum, status);
        if (aBoolean)
            return RespUtil.ok().setData(aBoolean);
        return RespUtil.fail().setData(aBoolean);
    }

    /**
     * 已通过 -> 回收站     ==================已测==================
     *
     * @param outNum
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "删除出库单失败", operation = "删除出库单")
    @PutMapping("/remove/{outNum}")
    @RequiresPermissions({"outStock:remove"})
    public RespUtil remove(@PathVariable("outNum") String outNum) throws IllegalAccessException {
        // 回收站
        int statusRec = 1;
        Boolean aBoolean = outStockService.updateStatus(outNum, statusRec);
        if (aBoolean)
            return RespUtil.ok().setData(aBoolean);
        return RespUtil.fail().setData(aBoolean);
    }


}
